#include <math.h>
#include <iostream>

using std::cout;
using std::endl;

class Point;//类的前向声明, 先告诉编译器Point是一个自定义类类型

class Line {
public:
    //成员函数
    //虽然是使用了Point的前向声明，但在distance函数的声明中，
    //我们不需要知道Point的内部成员, 因为引用只占用一个指针的大小
    //空间是确定的
    double distance(const Point & lhs, const Point & rhs);

    void setX(Point & pt, int ix);

private:
    int _iz;
};

class Point {
public:
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy)
    {
        cout << "Point(int,int)" << endl;
    }

    void print() const
    {
        cout << "(" << _ix 
             << "," << _iy
             << ")" << endl;
    }

    //Point不是Line的友元,无法访问Line中的私有成员
    void setZ(Line & line, int iz)
    {
        //line._iz = iz;//error
    }

    //类Line声明为Point的友元
    friend class Line;//友元类
    
private:
    int _ix;
    int _iy;
};

double Line::distance(const Point & lhs, const Point & rhs) {
    return sqrt((lhs._ix - rhs._ix) * (lhs._ix - rhs._ix)  + 
                (lhs._iy - rhs._iy) * (lhs._iy - rhs._iy));
}

void Line::setX(Point & pt, int ix)
{
    pt._ix = ix;
}

int main()
{
    cout << "class as friend:" << endl;
    Point pt1(1, 2), pt2(3, 4);
    pt1.print(); 
    cout << " ---> ";
    pt2.print();
    cout << "的距离是:" << Line().distance(pt1, pt2);
    cout << endl;

    Line().setX(pt1, 10);
    pt1.print();

    return 0;
}

